package com.demo.shardingsphere.order.config;


import com.demo.shardingsphere.util.OrderUtil;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
import java.util.Collection;

/**
 * 根据订单号查询分表路由
 */
public class OrderNoQueryTableAlgorithm implements PreciseShardingAlgorithm<String> {

    /**
     * 分表规则  采用mod 2^n 这种一致性Hash ,做多可分 8192表
     * @param availableTargetNames
     * @param shardingValue
     * @return
     */
    @Override
    public String doSharding(Collection<String> availableTargetNames,
                             PreciseShardingValue<String> shardingValue) {
        String orderNo = shardingValue.getValue();
        Integer uidSuffix= OrderUtil.parseUidSuffix(orderNo);
        //每个库2张表
        String tableName= "t_order"+uidSuffix%2;
        return tableName;

    }
}